from __future__ import absolute_import, unicode_literals
import os
from celery import Celery, platforms
from django.conf import settings

# from celery.schedules import crontab
from iClouds import celerycfg
from datetime import timedelta

# 设置django环境
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "iClouds.settings")

# 创建celery app
app = Celery("iClouds")

#  使用CELERY_ 作为前缀，在settings中写配置
# app.config_from_object("django.conf:settings", namespace="CELERY")
app.config_from_object(celerycfg)

# 发现任务文件每个app下的task.py
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
# app.autodiscover_tasks()

app.conf.update(
    CELERYBEAT_SCHEDULE={
        # "myTaskDoTest": {  # 自动点检未提交的事务
        #     "task": "mysqldb.tasks.myTaskDoTest",
        #     "schedule": timedelta(minutes=2),  # 系统默认UTC时区
        #     "args": (1, 10),
        # },
        # "audio_Controller": {
        #     "task": "rebot.tasks.audio_Controller",
        #     "schedule": timedelta(minutes=2),
        # },
        # "video_Controller": {
        #     "task": "rebot.tasks.video_Controller",
        #     "schedule": timedelta(minutes=4),
        # },
        # "lipsync_Controller": {
        #     "task": "rebot.tasks.lipsync_Controller",
        #     "schedule": timedelta(minutes=2),
        # },
        # "opencv_Controller": {
        #     "task": "rebot.tasks.opencv_Controller",
        #     "schedule": timedelta(minutes=5),
        # },
    }
)

# 解决celery不能root用户启动的问题
platforms.C_FORCE_ROOT = True

# 调用异步任务,返回任务ID
# eta = datetime.utcnow() + timedelta(seconds=10)
# res=MyCeleryTest.delay(3,4)
# res=MyCeleryTest.apply_async(args=[3,4], eta=eta)
# res=MyCeleryTest.apply_async(kwds={'a':4,'b':4}, eta=eta)
